package sailing.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cjlite.logger.Logger;

/**
 * @author ming
 */
public final class Utils {

	private static Logger logger = Logger.thisClass();
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	private static final SimpleDateFormat onlyDateFormat = new SimpleDateFormat("yyyy-MM-dd");


	/**
	 * 
	 */
	private Utils() {
	}


	public static String onlyDateFormat(long time) {
		try {
			return onlyDateFormat.format(new Date(time));
		} catch (Exception e) {
			logger.error("error on parsing long to date string", e, time);
		}
		return "";
	}


	public static String dateFormat(long createTime) {
		try {
			return sdf.format(new Date(createTime));
		} catch (Exception e) {
			logger.error("error on parsing long to date string", e, createTime);
		}
		return "";
	}


	/**
	 * @param dateStr (format yyyy-MM-dd)
	 * @return
	 */
	public static Timestamp dateParse(String dateStr) {
		if (dateStr == null || dateStr.trim().length() == 0) {
			return null;
		}

		String[] dates = dateStr.split("-");
		if (dates.length != 3) {
			return null;
		}
		try {
			int year = Integer.parseInt(dates[0]);
			int month = Integer.parseInt(dates[1]) - 1;
			int day = Integer.parseInt(dates[2]);
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month, day);
			return new Timestamp(calendar.getTimeInMillis());
		} catch (Exception e) {
			logger.error("error on parse String[{0}] to timestamp", e, dateStr);
		}

		return null;
	}

}
